package com.chao.yyks.modular.config.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chao.common.util.StrUtile;
import com.chao.common.util.exception.SysException;
import com.chao.common.util.reaction.PageList;
import com.chao.common.util.redis.SysRedis;
import com.chao.yyks.modular.config.bean.Dic;
import com.chao.yyks.modular.config.mapper.DicMapper;
import com.chao.yyks.modular.config.service.DicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.chao.common.util.SysConstant.DIC;

/**
 * 数据字典ServiceImpl
 * chao
 * 2020-08-05
 */
@Service
public class DicServiceImpl implements DicService {

	/**
	 * 数据字典Mapper
	 */
	@Autowired
	private DicMapper dicMapper;

	/**
	 * 新增数据字典
	 */
	@Override
	public void insertDic(Dic dic){
		if(dicMapper.selectDicByDicNum(dic) > 0){
			throw new SysException("字典编码或值重复,无法新增!");
		}
		dic.setDicId(StrUtile.getId());
		dicMapper.insertDic(dic);
		redisDic(dic.getDicType());
	}

	/**
	 * 修改数据字典
	 */
	@Override
	public void updateDic(Dic dic){
		if(dicMapper.selectDicByDicNum(dic) > 0){
			throw new SysException("字典编码或值重复,无法修改!");
		}
		Dic dicPojo = dicMapper.selectDicByDicId(dic.getDicId());
		if(null == dicPojo){
		    throw new SysException("修改数据字典失败:记录不存在!");
		}
		dicMapper.updateDic(dic);
		redisDic(dic.getDicType());
	}

	/**
	 * 查询数据字典详情
	 */
	@Override
	public Dic selectDicByDicId(String dicId){
		return dicMapper.selectDicByDicId(dicId);
	}

	/**
	 * 查询数据字典列表
	 */
	@Override
	public JSONObject selectDicByList(String dicType){
		JSONObject json = new JSONObject();
		if(!StrUtile.isEmpty(dicType)){
			String[] dicTypes = dicType.split(",");
			for(String key:dicTypes){
				JSONArray dics = SysRedis.getValue(DIC+key);
				if(dics == null){
					redisDic(key);
					dics = SysRedis.getValue(DIC+key);
				}
				if(null == dics){
					continue;
				}
				JSONArray dicTree = new JSONArray();
				for (int i = 0; i < dics.size(); i++) {
					JSONObject dic = dics.getJSONObject(i);
					if(StrUtile.isEmpty(dic.getString("pdicCode"))){
						dicTree.add(setDicTree(dic,dics));
					}
				}
				json.put(key,dicTree);
			}
		}
		return json;
	}

	/**
	 * 封装字典树
	 */
	private static JSONObject setDicTree(JSONObject dic, JSONArray dics) {
		JSONArray childNode = new JSONArray();
		for (int i = 0; i < dics.size(); i++) {
			JSONObject dic1 = dics.getJSONObject(i);
			if(dic.getString("dicCode").equals(dic1.getString("pdicCode"))){
				childNode.add(setDicTree(dic1,dics));
			}
		}
		if(childNode.size() > 0){
			dic.put("childNode",childNode);
		}
		return dic;
	}


	/**
	 * 分页查询数据字典列表
	 */
	@Override
	public PageList<Dic> selectDicByPage(JSONObject json){
		return dicMapper.selectDicByList(StrUtile.getPageJson(json));
	}

	/**
	 * 同步数据字典到redis
	 */
	@Override
	public void synchro(){
		List<Dic> dicList = dicMapper.selectDicByList(new JSONObject()).getList();
		Set<String> sets = new HashSet<>();
		dicList.forEach(dic -> sets.add(dic.getDicType()));
		sets.forEach(this::redisDic);
	}

	/**
	 * 查询数据字典类型列表
	 */
	@Override
	public List<JSONObject> selectDicTypeByList(){
		return dicMapper.selectDicTypeByList();
	}

	/**
	 * 缓存字典列表
	 */
	public void redisDic(String dicType){
		JSONObject js = new JSONObject();
		js.put("dicType",dicType);
		List<Dic> dicList = dicMapper.selectDicByList(js).getList();
		//JSONObject json = new JSONObject();
		//dicList.forEach(dic -> json.put(dic.getDicCode(),dic.getDicValue().trim()));
		SysRedis.setValue(DIC+dicType, JSONArray.toJSON(dicList));
	}
}
